<template>
  <div>
      {{count}}
      <button @click="destroy">点击卸载</button>
  </div>
</template>

<script>
import PubSub from 'pubsub-js'
export default {
    name:"Gege",
    data(){
        return{
            count:1,
            subId:''
        }
    },
    methods:{
        destroy(){
            this.$destroy()
        }
    },
    mounted(){
        // 回调的此一个参数是发布订阅的名字
        // 每个订阅都有自己的token,类似于定时器
      this.subId= PubSub.subscribe("count",(_,count)=>{
            this.count=count
        })
    },
    beforeDestroy(){
        // 取消订阅时把该id传入可指定取消订阅的对象
        PubSub.unsubscribe(this.subId)
    }
}
</script>

<style>

</style>